package com.atguigu01.collection;

import java.util.HexFormat;

/**
 * ClassName: test1
 * Package: com.atguigu01.collection
 * Description:
 *
 * @Author BuTang
 * @Create 2023/6/7 16:13
 * @Version 1.0
 */
public class test1 {

    //十六位二进制最大数值
    static int HexBinary_Max = 65535;
    static long DoubleHexBinary_Max = 4294967295l;


    public static void main(String[] args) {

        //这个只是测试
        //System.out.println(Integer.toBinaryString(100));

      // System.out.println(test1.decimalToBinary(25088));

        System.out.println(test1.unsignedToSigned1(65436));
        System.out.println(test1.unsignedToSigned1(65436)/100);
        System.out.println();
        System.out.println(test1.unsignedToSigned(4294967196l));
    }

    //Long.MAX_VALUE

    //十进制转换为十六位二进制并自动补齐
    public static String decimalToBinary(long n) {

        if (n < 0 || n >  HexBinary_Max) {
            throw new IllegalArgumentException("The input number must be between 0 and 65535.");
        }
        String binary = Long.toBinaryString(n);
        while (binary.length() < 16) {
            binary = "0" + binary;
        }
        return binary;
    }

    //十进制转换为三十二位二进制并自动补齐
    public static String decimalToBinary1(long n) {

        if (n < 0 || n >  DoubleHexBinary_Max) {
            throw new IllegalArgumentException("The input number must be between 0 and 65535.");
        }
        String binary = Long.toBinaryString(n);
        while (binary.length() < 32) {
            binary = "0" + binary;
        }
        return binary;
    }


    //无符号32位十进制转换为有符号十进制
    public static long unsignedToSigned(long unsigned) {
        if (unsigned <= 2147483647) {
            return unsigned;
        } else {
            return -(DoubleHexBinary_Max + 1 - unsigned);
        }
    }

    //无符号十六位十进制转换为有符号十进制

    public static long unsignedToSigned1(int unsigned) {
        if (unsigned <= 32767) {
            return unsigned;
        } else {
            return -(HexBinary_Max  + 1 - unsigned);
        }
    }
}